/**
 * Created with IntelliJ IDEA.
 * User: Bluishoul
 * Date: 13-10-22
 * Time: 下午4:38
 */
var Velocity = require("velocityjs");
var fs = require('fs');

var ExpressVelocity = {};

ExpressVelocity.render = function(template,context,fn){
	try{
		if(!!context && !context.Tools)
			context.Tools = require('./toolbox.js');
		var t1   = Date.now();
		var str = context.cache
			? exports.cache[template] || (exports.cache[template] = fs.readFileSync(template, 'utf8'))
			: fs.readFileSync(template, 'utf8');
		var tpl = Velocity.render(str,context,context && context.macros);
		var t2   = Date.now();
		var cost = t2 - t1;
		console.log("Generated tpl in "+cost+"[ms]");
		tpl +="<!-- Generated by VelocityJS (page:" +cost+ "[ms]) -->";
		fn(null, tpl);
	} catch (err) {
		fn(err);
	}
};

module.exports = ExpressVelocity;
